package com.bolins.examples.subspringside.mongodb;

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;

/**
 * 查询过滤器
 * 
 * @author liuys
 * 
 */
public class Restrictions {
	private static Map<String, Object> restrictions = new LinkedHashMap<String, Object>();;

	private Restrictions() {
	}

	public static void clear() {
		restrictions.clear();
	}

	public static void isEmpty() {
		restrictions.isEmpty();
	}

	public static void put(String key, Object value) {
		restrictions.put(key, value);
	}

	public static void append(String key, Object value) {
		restrictions.put(key, value);
	}

	public static void putAll(Map<String, Object> map) {
		restrictions.putAll(map);
	}

	public static void remove(String o) {
		restrictions.remove(o);
	}

	public static void idEq(Object value) {
		restrictions.put("id", value);
	}

	public static void eq(String propertyName, Object value) {
		restrictions.put(propertyName, value);
	}

	public static void ne(String propertyName, Object value) {
		Map<String, Object> restriction = new LinkedHashMap<String, Object>();
		restriction.put("$ne", value);
		restrictions.put(propertyName, restriction);
	}

	public static void like(String propertyName, Object value) {
		Pattern pattern = Pattern.compile(value.toString());
		restrictions.put(propertyName, pattern);
	}

	public static void like(String propertyName, String value,
			MatchMode matchMode) {
		Pattern pattern = Pattern.compile(matchMode.toMatchString(value));
		restrictions.put(propertyName, pattern);
	}

	public static void ilike(String propertyName, String value,
			MatchMode matchMode) {
		Pattern pattern = Pattern.compile(matchMode.toMatchString(value
				.toLowerCase()));
		restrictions.put(propertyName, pattern);
	}

	public static void ilike(String propertyName, Object value) {
		Pattern pattern = Pattern.compile(value.toString().toLowerCase());
		restrictions.put(propertyName, pattern);
	}

	public static void gt(String propertyName, Object value) {
		Map<String, Object> restriction = new LinkedHashMap<String, Object>();
		restriction.put("$gt", value);
		restrictions.put(propertyName, restriction);
	}

	public static void lt(String propertyName, Object value) {
		Map<String, Object> restriction = new LinkedHashMap<String, Object>();
		restriction.put("$lt", value);
		restrictions.put(propertyName, restriction);
	}

	public static void le(String propertyName, Object value) {
		Map<String, Object> restriction = new LinkedHashMap<String, Object>();
		restriction.put("$le", value);
		restrictions.put(propertyName, restriction);
	}

	public static void ge(String propertyName, Object value) {
		Map<String, Object> restriction = new LinkedHashMap<String, Object>();
		restriction.put("$ge", value);
		restrictions.put(propertyName, restriction);
	}

	public static void between(String propertyName, Object lo, Object hi) {
		Map<String, Object> restriction = new LinkedHashMap<String, Object>();
		restriction.put("$gt", lo);
		restriction.put("$lt", hi);
		restrictions.put(propertyName, restriction);
	}

	public static void in(String propertyName, Object[] values) {
		Map<String, Object> restriction = new LinkedHashMap<String, Object>();
		restriction.put("$in", values);
		restrictions.put(propertyName, restriction);
	}

	public static void in(String propertyName, Collection values) {
		Map<String, Object> restriction = new LinkedHashMap<String, Object>();
		restriction.put("$in", values.toArray());
		restrictions.put(propertyName, restriction);
	}

	public static void nin(String propertyName, Object[] values) {
		Map<String, Object> restriction = new LinkedHashMap<String, Object>();
		restriction.put("$nin", values);
		restrictions.put(propertyName, restriction);
	}

	public static void nin(String propertyName, Collection values) {
		Map<String, Object> restriction = new LinkedHashMap<String, Object>();
		restriction.put("$nin", values.toArray());
		restrictions.put(propertyName, restriction);
	}

	public static void isNull(String propertyName) {
		restrictions.put(propertyName, null);
	}

	public static void noProperty(String propertyName) {
		Map<String, Object> restriction = new LinkedHashMap<String, Object>();
		restriction.put("$exists", false);
		restrictions.put(propertyName, restriction);
	}

	public static void hasProperty(String propertyName) {
		Map<String, Object> restriction = new LinkedHashMap<String, Object>();
		restriction.put("$exists", true);
		restrictions.put(propertyName, restriction);
	}

	public static void eqProperty(String propertyName, String otherPropertyName) {
		// TODO
	}

	public static void neProperty(String propertyName, String otherPropertyName) {
		// TODO
	}

	public static void ltProperty(String propertyName, String otherPropertyName) {
		// TODO
	}

	public static void leProperty(String propertyName, String otherPropertyName) {
		// TODO
	}

	public static void gtProperty(String propertyName, String otherPropertyName) {
		// TODO
	}

	public static void geProperty(String propertyName, String otherPropertyName) {
		// TODO
	}

	public static void isNotNull(String propertyName) {
		Map<String, Object> restriction = new LinkedHashMap<String, Object>();
		restriction.put("$ne", null);
		restrictions.put(propertyName, restriction);
	}
/*
	public static void and(Criterion lhs, Criterion rhs) {
		// TODO
	}

	public static void or(Criterion lhs, Criterion rhs) {
		// TODO
	}

	public static void not(Criterion expression) {
		// TODO
	}
*/
	public static void sqlsRestriction(Map<String, Integer> propertyNameTypes) {
		// TODO
	}

	public static void typeRestriction(String propertyName, Integer type) {
	}

	public static void sqlRestriction(String sql) {
		// TODO
	}

	public static void conjunction() {
		// TODO
	}

	public static void disjunction() {
		// TODO
	}

	public static void allEq(Map<String, Object> propertyNameValues) {
		Set<String> names = propertyNameValues.keySet();
		for (Iterator<String> it = names.iterator(); it.hasNext();) {
			String key = it.next();
			Object value = propertyNameValues.get(key);
			restrictions.put(key, value);
		}
	}

	public static void isEmpty(String propertyName) {
		restrictions.put(propertyName, "");
	}

	public static void isNotEmpty(String propertyName) {
		Map<String, Object> restriction = new LinkedHashMap<String, Object>();
		restriction.put("$ne", "");
		restrictions.put(propertyName, restriction);
	}

	public static void sizeEq(String propertyName, int size) {
		Map<String, Object> restriction = new LinkedHashMap<String, Object>();
		restriction.put("$size", size);
		restrictions.put(propertyName, restriction);
	}

	public static void sizeNe(String propertyName, int size) {
		Map<String, Object> inRestriction = new LinkedHashMap<String, Object>();
		inRestriction.put("$size", size);
		Map<String, Object> outRestriction = new LinkedHashMap<String, Object>();
		inRestriction.put("$not", inRestriction);
		Map<String, Object> outRestriction2 = new LinkedHashMap<String, Object>();
		inRestriction.put("$exists", true);
		restrictions.put(propertyName, outRestriction);
		restrictions.put(propertyName, outRestriction2);
	}

	public static void sizeGt(String propertyName, int size) {
		// TODO
	}

	public static void sizeLt(String propertyName, int size) {
		// TODO
	}

	public static void sizeGe(String propertyName, int size) {
		// TODO
	}

	public static void sizeLe(String propertyName, int size) {
		// TODO
	}

	public static void naturalId() {
		// TODO
	}

	public Map<String, Object> toMap() {
		return restrictions;
	}

}
